﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace LogParser
{
    internal class StringParser
    {
        private int _index = 0;
        private string _currentLine = "";
        private Queue<string> _lines = new Queue<string>();

        internal void Feed( string line )
        {
            _lines.Enqueue( line );
        }

        public bool IsEmpty
        {
            get
            {
                return _lines.Count == 0 && _index >= _currentLine.Length;
            }
        }

        internal int Index
        {
            get { return _index; }
        }

        internal string CurrentLine
        {
            get
            {
                if (string.IsNullOrEmpty( _currentLine ) || _index >= _currentLine.Length)
                {
                    _currentLine = (_lines.Count == 0) ? string.Empty : _lines.Dequeue();
                    _index = 0;
                }

                return _currentLine;
            }
        }

        internal char PeekChar()
        {
            return CurrentLine[Index];
        }

        internal bool PeekString(string peek, bool ignoreCase)
        {
            if (CurrentLine.Length - _index < peek.Length)
                return false;

            return string.Compare( CurrentLine.Substring( _index, peek.Length ), peek, ignoreCase ) == 0;            
        }

        internal char ReadChar()
        {
            return CurrentLine[_index++];
        }
        
        internal string ReadToEndOfLine()
        {
            var consume = _currentLine.Substring( _index );
            _index = 0;
            _currentLine = string.Empty;
            return consume;
        }

        internal IEnumerable<string> SplitLine( char delimit )
        {
            int index = 0;
            int next = 0;

            var text = ReadToEndOfLine();

            while (next < text.Length)
            {
                next = text.IndexOf( delimit, index );
                if (next == -1)
                    next = text.Length;

                var consume = text.Substring( index, next - index );
                index = next + 1;
                yield return consume;
            }          
        }

        internal void SkipWhiteSpace()
        {
            while (Char.IsWhiteSpace( PeekChar() )) { ReadChar();  }
        }

        internal void Skip( int length )
        {
            _index += length;
        }

        internal static bool TryParseDateTime(string date_time, out DateTime dateTime)
        {
            return  DateTime.TryParseExact( date_time, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime );
        }
    }
}
